package com.sky.controller.admin;

import com.sky.result.Result;
import com.sky.service.RepoetService;
import com.sky.vo.OrderReportVO;
import com.sky.vo.SalesTop10ReportVO;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDate;
import java.util.concurrent.locks.ReadWriteLock;

@RequestMapping("/admin/report")
@RestController
@Api("数据统计相关接口")
@Slf4j
public class ReportContorller {
    @Autowired
    RepoetService repoetService;

    @ApiOperation("营业额统计")
    @GetMapping("turnoverStatistics")
    public Result turnoverStatistics(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                                     @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
        TurnoverReportVO turnoverReportVO = repoetService.statistics(begin, end);
        log.info("接收的时间:{},{}", begin, end);

        return Result.success(turnoverReportVO);
    }
    @ApiOperation("用户统计接口")
    @GetMapping("userStatistics")
    public Result userStatistics(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                                 @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){
        UserReportVO userReportVO = repoetService.userStatistics(begin,end);
        return Result.success(userReportVO);
    }
    @ApiOperation("订单统计接口")
    @GetMapping("ordersStatistics")
    public Result ordersStatistics(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                                   @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){
        OrderReportVO orderReportVO = repoetService.ordersStatistics(begin,end);
        return Result.success(orderReportVO);
    }
    @ApiOperation("查询销量排名top10接口\n")
    @GetMapping("top10")
    public Result top10(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                        @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){
        SalesTop10ReportVO salesTop10ReportVO =  repoetService.top10(begin,end);
        return Result.success(salesTop10ReportVO);
    }
    @ApiOperation("导出运营数据接口")
    @GetMapping("export")
    public void export(HttpServletResponse response) throws IOException {
        repoetService.export(response);
    }
}
